<?php
	namespace app\controllers\nurse;
	use yii\web\Controller;
	use app\models\nurse\nurse;
	use app\models\nurse\Admin;
	/**
	* 登录控制器
	*/
	class LoginController extends Controller
	{
		public $layout = 'nurse/login';
		public $enableCsrfValidation = false;
		public function actions(){
			return [
				'captcha'=>[
					'class'=>'yii\captcha\CaptchaAction',
					'maxLength'=>4,
					'minLength'=>4,
					'width'=>80,
					'height'=>40,
				],
			];
		}
		public function actionIndex(){
			//创建对象并指定场景
			$admin =  new Admin(['scenario' => 'login']);
			if(\yii::$app->request->isPost&&$admin->load(\YII::$app->request->post()) &&$admin->validate()){
				//接受数据
				$post = \YII::$app->request->post('Admin');
				//查询该数据是否存在该账户
				$select = admin::find()->where(['account'=>$post['account']])->asArray()->one();
				//如果该信息不存在则弹出警告信息并跳转会登录界面
				if($select==null){
					echo "<script>alert('账户不存在！');</script>";
					//return $this->render('index',['model'=>$admin]);
				}
				else{
						//判断该条记录的密码和他传过来的加密后的密码是否一致
						if(\Yii::$app->getSecurity()->validatePassword($post['password'], $select['password'])) {
				    	echo "<script>alert('登陆成功');</script>";
				    	return $this->redirect(['nurse/system/index']);
						} 
						//不一致则证明出错
						else{
							echo "<script>alert('密码或者账户错误！');</script>";
						}
				}
			}
			return $this->render('index',['model'=>$admin]);
		}
		public function actionTest(){
			return $this->render('test');
		}
	}
?>